﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using S1.Models;
using S1.Repositories;

namespace S1.Services
{
    public class ProductService : IDisposable
    {
        private readonly ProductRepository _productRepository;
        public ProductService()
        {
            _productRepository = new ProductRepository();
        }

        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Product> GetAll()
        {
            return _productRepository.All();
        }

        /// <summary>
        /// 通过Id获取一个产品
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <returns></returns>
        public Product Get(int id)
        {
            return _productRepository.Single(id);
        }

        /// <summary>
        /// 添加一个新产品
        /// </summary>
        /// <param name="product">新产品对象</param>
        /// <returns></returns>
        public bool Add(Product product)
        {
            return _productRepository.Add(product);
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="id">要更新的产品Id</param>
        /// <param name="product">更新后的产品对象</param>
        /// <returns></returns>
        public bool Update(int id,Product product)
        {
            return _productRepository.Update(id,product);
        }

        /// <summary>
        /// 根据ID，删除一个产品
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns></returns>
        public bool Remove(int id)
        {
            return _productRepository.RemoveSingle(id);
        }
        public void Dispose()
        {
            _productRepository.Dispose();
        }
    }
}